package cn.edu.njupt.eclass.aixinling.service.Impl;

import cn.edu.njupt.eclass.aixinling.domain.BaseResponse;
import cn.edu.njupt.eclass.aixinling.mapper.PictureMapper;
import cn.edu.njupt.eclass.aixinling.model.Picture;
import cn.edu.njupt.eclass.aixinling.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: kent
 * @date: 2018/6/4 20:53
 */
@Service("PictureService")
public class IPictureService implements PictureService{
    /**
     * windows的图片存储路径
     */
    private static final String PATH_WIN = "C:/testsell_files/";
    /**
     * MAC的图片存储路径
     */
    private static final String PATH_MAC = "/Users/wangdonghai/Documents/Pictures/testsell_files/";
    /**
     * Linux的图片存储路径
     */
    private static final String PATH_LINUX = "/usr/testsellpic/";
    /**
     * 当前的图片存储路径
     */
    private static  String PATH_NOW="";
    /**
     * 根据系统判断使用哪个图片存储路径
     */
    static{



        String systemStr = System.getProperty("os.name").toLowerCase();
        System.out.println("now system = " + systemStr);

        if(systemStr.contains("windows")){
            PATH_NOW = PATH_WIN;
        }else{
            if(systemStr.contains("mac")){
                PATH_NOW = PATH_MAC;
            }else{
                PATH_NOW = PATH_LINUX;
            }
        }

        File folder = new File(PATH_NOW);
        try{
            if(!folder.exists()){
                folder.mkdirs();
            }
        }catch(Exception e){
            e.printStackTrace();

        }
        System.out.println("now path = " + PATH_NOW);
    }

    /**
     * 上传图片
     * @param file
     * @return 图片上传状态，若成功，则返回picId
     */
    @Override
    public BaseResponse upload(MultipartFile file) {

        // TODO 校验
        if (null == file || file.getSize() <= 0) {
            return BaseResponse.badrequest("未选择文件！");
        }

        String fileName = file.getOriginalFilename();

        // 图片扩展名
        String ext = fileName.substring(fileName.lastIndexOf("."));
        String picId = UUID.randomUUID().toString() + ext;

        // 保存图片到硬盘
        File targetFile = new File(PATH_NOW, picId);

        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();

            return BaseResponse.error("保存图片失败！");
        }


        Map<String, String> returnValues = new HashMap<String, String>();
//        returnValues.put("picUrl", ProblemController.getPicUrl(picId));
        returnValues.put("picId", picId);

        return BaseResponse.success("上传成功",returnValues);

    }

    /**
     * 获取图片
     * @param picId
     * @param response
     */
    @Override
    public void getPic(String picId, HttpServletResponse response) {


        FileInputStream fis = null;
        try {

            File file = new File(PATH_NOW + picId);
            //获取图片格式
            String ext = picId.substring(picId.indexOf(".") + 1);

            response.setContentType("image/" + ext);

            fis = new FileInputStream(file);
            byte[] b = new byte[fis.available()];
            fis.read(b);
            OutputStream out = response.getOutputStream();
            out.write(b);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
